<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/25
  Time: 11:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>患者</title>
    <link href="${pageContext.request.contextPath}/layui/css/layui.css" type="text/css" rel="stylesheet" media="all"/>
    <style type="text/css">
        .layui-table-cell{
            height: auto;
            line-height: 28px;
        }

    </style>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <script type="text/javascript">
        layui.config({
            version: true //一般用于更新组件缓存，默认不开启。设为true即让浏览器不缓存。也可以设为一个固定的值，如：201610
        });
        //首页展示
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: '#myTable',//一般是id选择器
                url:"patient/showPage",//数据接口，请求地址
                page: true,//开启分页
                toolbar:"#myToolbar",//顶部工具栏
                cols: [[ //表头
                    {type:"checkbox",align:"center"},
                    {field: 'patientid',title:'编号'},
                    {field: 'patientname', title: '患者名字'},
                    {field: 'patientsex', title: '患者性别',templet:'#priceTemplet'},
                    {field: 'birth', title: '患者生日'},
                    {field: 'idcard', title: '患者身份证号'},
                    {field: 'address', title: '家庭住址'},
                    {field: 'mobile', title: '手机号码'},
                    {title:'操作',toolbar:"#caozuo"}
                ]]
            });
        });
        /*显示弹出层*/
        function openLayer() {
            $("#patientid").val("0")
            $("#myForm")[0].reset()
            layui.use('layer', function(){
                var layer = layui.layer;
                //layer.msg('hello');
                layer.open({
                    type:1,
                    content:$("#myForm"),
                    area: ['500px', '300px'],
                    maxmin:true
                })
            });
        }
        function tanchu(){
            layui.use('layer', function(){
                var layer = layui.layer;
                //layer.msg('hello');
                layer.open({
                    type:1,
                    content:$("#myForm2"),
                    area: ['500px', '300px'],
                    maxmin:true
                })
            });
        }
        //执行添加和修改操作

        layui.use(['layer','form','table'], function() {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            form.on("submit(update)",function (data) {
                //console.log(data.field)
                if (data.field.patientid!=0){
                    //执行修改
                    $.ajax({
                        url:"patient/updatePatient",
                        data:data.field,
                        type:"post",
                        success:function (data) {
                            layer.closeAll('page')
                            if (data.updateResult){
                                layer.alert("修改成功",{time:3000})
                            } else {
                                layer.alert("修改失败",{time:3000})
                            }
                            table.reload("myTable")
                        }
                    })
                } else{
                    //执行添加
                    console.log(data.field)
                    $.ajax({
                        url:"patient/addPatient",
                        data:data.field,
                        type:"post",
                        success:function (data) {

                            layer.closeAll('page')
                            if (data.addResult){
                                layer.alert("添加成功",{time:3000})
                            } else {
                                layer.alert("添加失败",{time:3000})
                            }
                            table.reload("myTable")
                        }
                    })
                }
            })
            form.on("submit(add)",function (data) {
                $.ajax({
                    url:"triage/add",
                    data:data.field,
                    type:"post",
                    success:function (data) {

                        layer.closeAll('page')
                        if (data.addResult){
                            layer.alert("添加成功",{time:3000})
                        } else {
                            layer.alert("添加失败",{time:3000})
                        }
                        table.reload("myTable")
                    }
                })
            })
        })

        /*搜索*/
        function doSeach() {
            //1.获取输入框的内容
            var patientname = $("#name").val()
            //2.发送请求，并且接受数据
            layui.use('table', function() {
                var table = layui.table;

                //第一个实例
                table.reload('myTable',{
                    where:{"patientname":patientname}
                })
            });
        }

        /*删除*/
        layui.use('table', function() {
            var table = layui.table;
            //监听工具条
            table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                //console.log(data.bookid)
                if(layEvent === 'detail'){ //查看
                    //do somehing
                } else if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        layer.close(index);
                        //向服务端发送删除指令
                        $.ajax({
                            url:"patient/deletePatient",
                            data:"id="+data.patientid,
                            type:"post",
                            success:function (data) {
                                if (data.deletResult){
                                    layer.alert("删除成功",{time:3000})
                                } else {
                                    layer.alert("删除失败",{time:3000})
                                }
                                table.reload("myTable")
                            }
                        })
                    });
                } else if(layEvent === 'edit'){ //编辑
                    //do something
                    openLayer()
                    $.ajax({
                        url:"patient/selectOne",
                        data:"id="+data.patientid,
                        type:"post",
                        success:function (data) {
                            $("#patientid").prop("value",data.patient.patientid);
                            $("#patientname").val(data.patient.patientname);
                            if (data.patient.patientsex==1){
                                $("#man").prop("checked","checked");
                            } else {
                                $("#woman").prop("checked","checked");
                            }
                            $("#birth").val(data.patient.birth);
                            $("#idcard").val(data.patient.idcard);
                            $("#address").val(data.patient.address);
                            $("#mobile").val(data.patient.mobile);
                        }
                    })
                }else if(layEvent === 'gua'){
                    $("#id").val(data.patientid);
                    $("#name2").val(data.patientname);
                    tanchu();
                }
            });
        })

        //批量删除
        function doDelete(){
            layui.use(['table','layer'], function() {
                var table = layui.table;
                var layer = layui.layer;
                var checkStatus = table.checkStatus('myTable'); //idTest 即为基础参数 id 对应的值
                //console.log(checkStatus.data) //获取选中行的数据
                //console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                var students = checkStatus.data;
                console.log(students)
                if (students.length==0){
                    layer.alert("请选择要删除的数据")
                } else {
                    layer.confirm('真的删除行么', function(index) {
                        layer.close(index);
                        //向服务端发送删除指令
                        var ids = Array(students.length);
                        for (var i = 0; i < students.length; i++) {
                            ids[i] = students[i].patientid
                        }
                        $.ajax({
                            url: "patient/deleteSome",
                            data: "ids=" + ids,
                            success: function (data) {
                                layer.alert(data.deleteResult)
                                table.reload("myTable")
                            }
                        })
                    })
                }
            })
        }
        //时间
        layui.use('laydate', function(){
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#birth' //指定元素
            });
        });
        $(function () {
            var str = "";
            $.ajax({
                url:"${pageContext.request.contextPath}/department/showAll?id="+new Date(),
                success:function (data) {
                    for (var i=0;i<data.length;i++){
                        str += "<option value='"+data[i].id+"'>"+data[i].title+"</option>";
                    }
                    $("#slist").html(str);
                }
            })
        })
    </script>
    <script type="text/html" id="priceTemplet">
        {{#  if(d.patientsex == 1){ }}
        男
        {{#  } else { }}
        女
        {{#  } }}
    </script>
</head>
<body>
    <table id="myTable" lay-filter="test"></table>

    <div id="myToolbar" style="display: none">
        <button class="layui-btn" type="button" onclick="openLayer()">
            <i class="layui-icon">&#xe608;</i>添加
        </button>
        <button type="button" class="layui-btn layui-btn-radius layui-btn-danger" onclick="doDelete()">
            <i class="layui-icon layui-icon-delete"></i>批量删除
        </button>
        <div class="layui-inline">
            <label class="layui-form-label">患者名</label>
            <div class="layui-input-inline" style="width: 200px;">
                <input type="text" class="layui-input" id="name">
            </div>
            <div class="layui-input-inline" style="width: 100px;">
                <button class="layui-btn layui-btn-normal" type="button" onclick="doSeach()">
                    <i class="layui-icon layui-icon-search"></i>
                </button>
            </div>
        </div>
    </div>

    <div id="caozuo" style="display: none">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-event="del">删除</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-event="gua">挂号</button>
        </div>
    </div>
    <%--弹出层--%>
    <form class="layui-form" id="myForm" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">患者名字</label>
            <div class="layui-input-block">
                <input type="text" name="patientname" id="patientname" required  lay-verify="required" class="layui-input">
                <input value="0" type="hidden" name="patientid" class="layui-input" id="patientid">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">患者性别</label>
            <div class="layui-input-block">
                男<input type="radio" name="patientsex" value="1" checked id="man">
                女<input type="radio" name="patientsex" value="0" id="woman">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">患者生日</label>
            <div class="layui-input-block">
                <input type="text" id="birth" name="birth" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">患者身份证号</label>
            <div class="layui-input-block">
                <input type="text" id="idcard" name="idcard" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">家庭住址</label>
            <div class="layui-input-block">
                <input type="text" id="address" name="address" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" id="mobile" name="mobile" required  lay-verify="required" class="layui-input">
            </div>
        </div>

        <button class="layui-btn" type="button" lay-submit lay-filter="update">确认</button>
        <button class="layui-btn" type="reset">重置</button>
    </form>

    <form class="layui-form" id="myForm2" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" name="patient.patientid" id="id" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名字</label>
            <div class="layui-input-block">
                <input type="text" name="patient.patientname" id="name2" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">科室</label>
            <div class="layui-input-block">
                <select id="slist" name="department.departmentid">

                </select>
            </div>
        </div>

        <button class="layui-btn" type="button" lay-submit lay-filter="add">挂号</button>
        <button class="layui-btn" type="button">取消</button>
    </form>
</body>
</html>
